package controller.admin;

import model.User;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AdminUserEditServlet", value = "/admin/user_edit")
public class AdminUserEditServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService us = new UserService();
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");

        //必要的参数没有
        if (!(id != null && !"".equals(id))) {
            response.sendRedirect(request.getHeader("Referer"));
            return;
        }
        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setName(name);
        user.setPhone(phone);
        user.setAddress(address);

        boolean flag = us.updateUserAddress(user);
        if (flag) {
            request.setAttribute("msg", "修改成功！");
        } else {
            request.setAttribute("failMsg", "修改失败！");
        }
        request.getRequestDispatcher("user_list").forward(request, response);
    }
}
